package com.zcgk.core.exception;

import com.alibaba.fastjson.JSONObject;
import com.zcgk.core.domain.CodeMsg;
import com.zcgk.core.domain.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/****************************************************
 *
 * 异常处理
 *
 * @author Francis
 * @date 2020/2/3 22:48
 * @version 1.0
 **************************************************/
@Slf4j
@ResponseBody
@ControllerAdvice
public class MyExceptionHandler {

    /**
     * 封装参数校验失败的返回字段
     *      传参为 json 时生效，即：controller接收参数有@RequestBody注解(此时前端请求方式为：axiosjson)
     *
     * @param ex MethodArgumentNotValidException
     * @return json
     * @author Francis
     * @date 2020-02-03 22:50
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public JsonResult<JSONObject> handleMethodArgumentNotValidException(MethodArgumentNotValidException ex){
        BindingResult br = ex.getBindingResult();

        List<FieldError> fieldErrors = br.getFieldErrors();
        JSONObject json = new JSONObject();

        for (FieldError error : fieldErrors) {
            json.put(error.getField(), error.getDefaultMessage());
        }

        return JsonResult.common(CodeMsg.PARAM_NOT_PASS.getCode(), CodeMsg.PARAM_NOT_PASS.getMsg(), json);
    }


    /**
     * 封装参数校验失败的返回字段
     *      常规传参时生效，即controller接收参数无@RequestBody注解(此时前端请求方式为：axios)
     *
     * @param br BindingResult
     * @return json
     * @author Francis
     * @date 2020-02-03 23:10
     */
    @ExceptionHandler(BindException.class)
    public JsonResult<JSONObject> handleMethodArgumentNotValidException(BindingResult br){
        List<FieldError> fieldErrors = br.getFieldErrors();
        JSONObject json = new JSONObject();

        for (FieldError error : fieldErrors) {
            json.put(error.getField(), error.getDefaultMessage());
        }

        return JsonResult.error(CodeMsg.PARAM_NOT_PASS, json);
    }


    /**
     * 自定义异常
     *
     * @param ex CosyException
     * @return json
     * @author Francis
     * @date 2020-02-03 23:20
     */
    @ExceptionHandler({CosyException.class})
    public JsonResult<JSONObject> handleBaseException(final CosyException ex) {
        return JsonResult.common(ex.getCode(), ex.getMsg(), null);
    }


    /**
     * 默认异常处理
     *
     * @param ex Exception
     * @return json
     * @author Francis
     * @date 2020-02-03 23:25
     */
    @ExceptionHandler(Exception.class)
    public JsonResult<String> defaultExceptionHandler(Exception ex) {
        log.error("拦截了异常信息：", ex);
        return JsonResult.error(CodeMsg.SERVICE_ERROR, ex.getMessage());
    }

}